import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreCaptchaService extends BaseService<any> {
  private readonly logger = new Logger(CoreCaptchaService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * createRecord (原方法名: create)
   * 对应 PHP: CoreCaptchaService_core::create()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async createRecord(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: create
    // PHP原文: $captcha_data = Captcha::create(null, true);          $captcha_key   = md5(uniqid('', true));         // 验证码10分钟有效         Cache::set($captcha_key, $c...
const captcha_data = Captcha.create(null, true);

        const captcha_key = md5(uniqid('', true)];
        // 验证码10分钟有效
        Cache.set(captcha_key, captcha_data.code, 600];
        return [
            captcha_key: captcha_key,
            img: captcha_data.img,
        );
    }

    /**
     * 核验验证码
     * @return true
     */
    async check(){
        const captcha_key = request().param('captcha_key', '');
        const captcha_code = request().param('captcha_code', '');
        if(!captcha_key) || !captcha_code)) throw new CaptchaBusinessException('CAPTCHA_ERROR'];
        const captcha = Cache.pull(captcha_key);
        if (!captcha)) throw new CaptchaBusinessException('CAPTCHA_ERROR');

        if (captcha_code != captcha) throw new CaptchaBusinessException('CAPTCHA_ERROR');

        return true;
    }
}
  }

  /**
   * check
   * 对应 PHP: CoreCaptchaService_core::check()
   * 逻辑类型: undefined - undefined
   */
  async check() {
    // 基于PHP真实逻辑: check
    // PHP原文: $captcha_key = request()->param('captcha_key', '');         $captcha_code = request()->param('captcha_code', '');         if(empty($captcha_key) || em...
const captcha_key = request().param('captcha_key', '');
        const captcha_code = request().param('captcha_code', '');
        if(!captcha_key) || !captcha_code)) throw new CaptchaBusinessException('CAPTCHA_ERROR'];
        const captcha = Cache.pull(captcha_key);
        if (!captcha)) throw new CaptchaBusinessException('CAPTCHA_ERROR');

        if (captcha_code != captcha) throw new CaptchaBusinessException('CAPTCHA_ERROR');

        return true;
    }
}
  }
}
